/*************************************************************************
	> File Name: test10.cc
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/09/21 11:56
 ************************************************************************/

#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

void showPending(sigset_t *pending)
{
    for (int i = 1; i <= 31; ++i) {
        if (sigismember(pending, i)) {
            std::cout << 1;
        } else {
            std::cout << 0;
        }
    }
    std::cout << std::endl;
}

void handler(int signum)
{
    std::cout << "Get a signal:" << signum << std::endl;
    sigset_t pending;
    for (int i = 0; i < 20; ++i) {
        sigpending(&pending);
        showPending(&pending);
        sleep(1);
    }
}

int main(int argc, char *argv[])
{
    std::cout << "pid:" << getpid() << std::endl;
    struct sigaction act, oact;
    act.sa_flags = 0;
    sigemptyset(&act.sa_mask);
    act.sa_handler = handler;
    sigaddset(&act.sa_mask, 3);
    sigaddset(&act.sa_mask, 4);
    sigaddset(&act.sa_mask, 5);
    sigaddset(&act.sa_mask, 6);
    sigaddset(&act.sa_mask, 7);
    sigaction(2, &act, &oact);
    std::cout << "Default action:" << (int)(oact.sa_handler) << std::endl;
    while (1) {
        sleep(1);
    }
    return 0;
}
